package cn.samehope.pet.interceptor;

import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.weixin.sdk.api.JsTicket;
import com.jfinal.weixin.sdk.api.JsTicketApi;
import com.jfinal.weixin.sdk.api.JsTicketApi.JsApiType;

/**
 * 微信JSSDK 参数拦截器
 * @author LD
 *
 */
public class WxJsSdkInterceptor implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		HttpServletRequest request = controller.getRequest();
		
		// TODO Auto-generated method stub
		String _wxTimestamp = (System.currentTimeMillis() / 1000) + "";
		String _wxNoncestr = StrKit.getRandomUUID();
		
		String _wxJsApiTicket = "";
	    //JsTicket jsTicket = JsTicketApi.getTicket(JsApiType.jsapi);
	    
//		String jsonTiket = HttpKit.get(PropKit.get("jsonTiketUrl"));
//		JsTicket jsTicket = new JsTicket(jsonTiket);;
//		_wxJsApiTicket    = jsTicket.getTicket();
		
		//获取TICKET
		JsTicket jsTicket = JsTicketApi.getTicket(JsApiType.jsapi);
		_wxJsApiTicket    = jsTicket.getTicket();
	    
//	    String _wxShareUrl = request.getHeader("Referer");
//	    if (StrKit.notBlank(_wxShareUrl)) {
//	        _wxShareUrl = _wxShareUrl.split("#")[0];
//	    }
	    
		//获取请求相关链接
	    String getRequestURL =request.getRequestURL().toString();
	    String queryurl=request.getQueryString();
		if(null!=queryurl){
			getRequestURL += "?" + queryurl;
		}
		String _wxShareUrl = "";
		if (StrKit.notBlank(getRequestURL)) {
	        _wxShareUrl = getRequestURL.split("#")[0];
	    }
		    
		Map<String, String> _wxMap = new TreeMap<String, String>();
		//参数封装
		_wxMap.put("noncestr", _wxNoncestr);
		_wxMap.put("timestamp", _wxTimestamp );
		_wxMap.put("jsapi_ticket", _wxJsApiTicket);
		_wxMap.put("url", _wxShareUrl);
		
		// 加密获取signature
		StringBuilder _wxBaseString = new StringBuilder();
		for (Entry<String, String> param : _wxMap.entrySet()) {
		    _wxBaseString.append(param.getKey()).append("=").append(param.getValue()).append("&");
		}
		String _wxSignString = _wxBaseString.substring(0, _wxBaseString.length() - 1);
		// signature
		String _wxSignature = HashKit.sha1(_wxSignString);
		
		request.setAttribute("appId", PropKit.get("appId"));
		request.setAttribute("timestamp", _wxTimestamp);
		request.setAttribute("noncestr", _wxNoncestr);
		request.setAttribute("signature", _wxSignature);
		
		LogKit.info("----------appId------" + PropKit.get("appId"));
		LogKit.info("----------_wxJsApiTicket------" + _wxJsApiTicket);
		LogKit.info("----------_wxShareUrl------" + _wxShareUrl);
		LogKit.info("----------timestamp------" + _wxTimestamp);
		LogKit.info("----------_wxNoncestr------" + _wxNoncestr);
		LogKit.info("----------_wxSignature------" + _wxSignature);
		inv.invoke();
	}

}
